<?php

namespace app\api\controller;

use app\admin\model\Aboutus;
use app\admin\model\Address;
use app\admin\model\Article;
use app\admin\model\Banner;
use app\admin\model\Classify;
use app\admin\model\Detail;
use app\admin\model\Details;
use app\admin\model\Matter;
use app\admin\model\Mine;
use app\admin\model\Myorder;
use app\admin\model\Nav;
use app\admin\model\Notice;
use app\admin\model\Opinion;
use app\admin\model\Order;
use app\admin\model\Orderdetails;
use app\admin\model\Paren;
use app\admin\model\Shop;
use app\admin\model\Shopcar;
use app\admin\model\Shopcate;
use app\admin\model\Unav;
use app\api\model\WxUser;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    //导航菜单列表
    public function nav()
    {
        // 接收页数
        $page = $this->request->param('page/d',1);
        // 接收每页显示的条数
        $pageSize = $this->request->param('page_size/d', 4);
        $nav = Nav::field('id,name,url,createtime')
            ->order(['id' => 'desc'])
            ->where('status', '=', 1)
            ->page($page,$pageSize)
            ->select();
        $this->success('导航菜单列表', $nav);
        //$this->success('请求成功');
    }

    //轮播图列表
    public function banner()
    {
        // 接收页数
        $page = $this->request->param('page/d',1);
        // 接收每页显示的条数
        $pageSize = $this->request->param('page_size/d', 3);
        $banner = Banner::field('id,image')->page($page,$pageSize)
            ->where('status', '=', 1)
            ->select();
        $this->success('轮播图', $banner);
    }

    //关于我们
    public function aboutus()
    {
        $aboutus = Aboutus::field('id,avatar,title,image,content,phone,email,')->select();
        $this->success('关于我们', $aboutus);
    }

    //地址
    public function address()
    {
        $id = input('wxuser_id/d');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $address = Address::where($id)->field('id,wxuser_id,name,phone,address,doorplate')
            ->where('status', '=', 1)
            ->select();
        $this->success('地址', $address);
    }

    //资讯详情
    public function article()
    {
        $id = input('id/d');
        $article = Article::where($id)->field('id,title,image,content,createtime')
            ->order('id','desc')
            ->where('status', '=', 1)
            ->find();
        $this->success('资讯详情', $article);
    }
    //资讯列表
    public function details()
    {
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 1);
        $details = Article::field('id,title,image,createtime')
            ->where('status', '=', 1)
            ->order('id','desc')
            ->page($page,$pageSize)
            ->select();
        $this->success('资讯列表', $details);
    }

    //常见问题
    public function matter()
    {
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 6);
        $matter = Matter::field('id,title,content')
            ->page($page,$pageSize)
            ->where('status', '=', 1)
            ->select();
        $this->success('常见问题', $matter);
    }

    //用户
    public function wxuser()
    {
        $id=input('id/d');
        $wxuser = WxUser::where($id)->field('id,wechat_id,mobile,avatar,nickname,price,status')
            ->where('status', '=', 1)
            ->select();
        $this->success('用户接口', $wxuser);
    }

    //用户导航栏
    public function unav()
    {
        $unav = Unav::field('id,name,url')
            ->where('status', '=', 1)
            ->select();
        $this->success('用户导航栏', $unav);
    }

    //公告
    public function notice()
    {
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 1);
        $notice = Notice::field('id,content')
            ->order(['id' => 'desc'])
            ->page($page,$pageSize)
            ->where('status', '=', 1)
            ->find();
        $this->success('公告', $notice);
    }
    //收支明细
    public function detail()
    {
        $id = input('wxuser_id/d');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 6);
        $detail = Detail::where($id)->field('id,wxuser_id,title,price,status')
            ->order('id','desc')
            ->where('status', '=', 1)
            ->page($page,$pageSize)
            ->select();
        $this->success('收支明细', $detail);
    }
    //订单
    public function order()
    {
        $status = input('status/d');
        $model = new Order();
        $where['wxuser_id'] = $this->auth->id;
        if (!empty($status)) $where['order.status'] = $status;
        $data = $model->where($where)
            ->field('list_order,createtime,updatetime',true)
            ->select();
        foreach ($data as $k => $v) {
            if (!empty($v['shop']['image'])) $data[$k]['shop']['image'] =  $v['shop']['image'];
        }
        $this->success('获取成功', $data);
    }
    //  订单详情接口
    public function orderdetails()
    {
        $order_num = input('num/s');
        //if (empty($order_num)) $this->error('请选择订单');
        $data = Orderdetails::where('num', $order_num)
            ->field('id,wxuser_id,shop_id,address_id,num,total,createtime')
            ->with(['shop', 'address'])
            ->find();
        $this->success('获取成功', $data);
    }

    //首页分类
    public function classify()
    {
        $page = input('page/d',1);
        $pageSize = input('pageSize/d',3);
        $data = Classify::where(['status' => 1])
            ->page($page,$pageSize)
            ->field('id,name,paren_id')
            ->select();
        $this->success('获取成功', $data);
    }
    //分类类型
    public function paren()
    {
        $page = input('page/d',1);
        $pageSize = input('pageSize/d',4);
        $data = Paren::where(['status' => 1])
            ->page($page,$pageSize)
            ->field('id,name')
            ->select();
        $this->success('获取成功', $data);
    }
    //商品
    public function shop()
    {
        $id = $this->request->param('id/d');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $list = Shop::where( $id)
            ->field('id,shop_cate_id,title,image,price')
            ->where('status','=',1)
            ->select();
        if (!empty($list)) {
            foreach ($list as &$v) {
                $v['image'] =  $v->image;
            }
        }
        $this->success('获取成功', $list);
    }

    //购物车列表显示
    public function shopcar()
    {
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 6);
        $model = new Shopcar();
        $data = $model->where(['status' => 1])
            ->field('id,shop_id,price,total')
            ->page($page,$pageSize)
            ->select();
        foreach ($data as &$v) {
            $v['image'] =  $v->shop->image;
            $v['shop_title'] = $v->shop->title;
            unset($v->shop);
        }
        $this->success('获取成功！', $data);
    }
    //商品分类
    public function shopcate()
    {
        $page = $this->request->param('page/d',1);
        $pageSize = $this->request->param('page_size/d', 4);
        $cate = Shopcate::where(['status' => 1])
            ->field('id,name')
            ->page($page,$pageSize)
            ->select();
        $this->success('获取成功', $cate);
    }
}
